package com.lsl.gulimall.ware.service.impl;

import com.lsl.common.utils.R;
import com.lsl.gulimall.ware.feign.ProductFeignService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsl.common.utils.PageUtils;
import com.lsl.common.utils.Query;

import com.lsl.gulimall.ware.dao.WmsWareSkuDao;
import com.lsl.gulimall.ware.entity.WmsWareSkuEntity;
import com.lsl.gulimall.ware.service.WmsWareSkuService;


@Service("wmsWareSkuService")
public class WmsWareSkuServiceImpl extends ServiceImpl<WmsWareSkuDao, WmsWareSkuEntity> implements WmsWareSkuService {

    @Autowired
    WmsWareSkuDao wmsWareSkuDao;
    @Autowired
    ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WmsWareSkuEntity> page = this.page(
                new Query<WmsWareSkuEntity>().getPage(params),
                new QueryWrapper<WmsWareSkuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        String wareId = (String) params.get("wareId");
        String skuId = (String) params.get("skuId");
        QueryWrapper<WmsWareSkuEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(wareId)) {
            wrapper.eq("ware_id", wareId);
        }
        if (StringUtils.isNotBlank(skuId)) {
            wrapper.eq("sku_id", skuId);
        }
        IPage<WmsWareSkuEntity> page = this.page(
                new Query<WmsWareSkuEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void addStock(Long wareId, Long skuId, Integer skuNum) {
        QueryWrapper<WmsWareSkuEntity> wmsWareSkuEntityQueryWrapper = new QueryWrapper<>();
        wmsWareSkuEntityQueryWrapper.eq("sku_id", skuId).eq("ware_id", wareId);
        List<WmsWareSkuEntity> wmsWareSkuEntities = this.baseMapper.selectList(wmsWareSkuEntityQueryWrapper);
        if (wmsWareSkuEntities != null && wmsWareSkuEntities.size() > 0) {
            wmsWareSkuDao.addStock(wareId, skuId, skuNum);
        } else {
            WmsWareSkuEntity wmsWareSkuEntity = new WmsWareSkuEntity();
            wmsWareSkuEntity.setSkuId(skuId);
            wmsWareSkuEntity.setStockLocked(0);
            wmsWareSkuEntity.setStock(skuNum);
            wmsWareSkuEntity.setWareId(wareId);
            //远程调用获取sku_name
            try {
                R info = productFeignService.getSkuInfo(skuId);
                Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");
                if (info.getCode() == 0) {
                    wmsWareSkuEntity.setSkuName((String) data.get("skuName"));
                }
            } catch (Exception e) {
            }
            this.baseMapper.insert(wmsWareSkuEntity);
        }
    }
}